function [datastruct, ndata] = get_ptclassids(ptstruct,whichgroup)
% Get patient ids for class defined in the group requested
% It also performs some basic analysis

N = length(ptstruct);
c = 0;
switch whichgroup
    case 'mort28d'
        
        for pt = 1 : N
            
            if ptstruct(pt).nicus(1) == 1
                % Process patients with only one ICU admission
                c = c+1;
                datastruct{c}.ptid = ptstruct(pt).id;
                datastruct{c}.iid = ptstruct(pt).nicus(2);
                datastruct{c}.mort = ptstruct(pt).clinics.mort28d;
                datastruct{c}.vaso = ptstruct(pt).clinics.vp;
                datastruct{c}.lvef = ptstruct(pt).clinics.lvef;
                datastruct{c}.age = ptstruct(pt).age;
                datastruct{c}.los = ptstruct(pt).los;
                datastruct{c}.male = ptstruct(pt).ismale;
                datastruct{c}.data{1} = ptstruct(pt).data{1}.hr;
                datastruct{c}.data{2} = ptstruct(pt).data{1}.pulsep;
                datastruct{c}.data{3} = ptstruct(pt).data{1}.lactate;
                datastruct{c}.data{4} = ptstruct(pt).data{1}.uo;
                datastruct{c}.data{5} = ptstruct(pt).data{1}.map;
            else
                % Process patients with more than one ICU admission
                nicus = ptstruct(pt).nicus(1);
                if length(ptstruct(pt).clinics.mort28d) ~= nicus
                    fprintf('\nPROBLEM in ptstruct\n');
                else
                    if sum(ptstruct(pt).clinics.mort28d) < 2
                        for i = 1 : nicus
                            c = c+1;
                            datastruct{c}.ptid = ptstruct(pt).id;
                            datastruct{c}.iid = ptstruct(pt).nicus(i+1);
                            datastruct{c}.mort = ptstruct(pt).clinics.mort28d(i);
                            datastruct{c}.vaso = ptstruct(pt).clinics.vp(i);
                            datastruct{c}.lvef = ptstruct(pt).clinics.lvef(i);
                            datastruct{c}.age = ptstruct(pt).age(i);
                            datastruct{c}.los = ptstruct(pt).los(i);
                            datastruct{c}.male = ptstruct(pt).ismale;
                            datastruct{c}.data{1} = ptstruct(pt).data{i}.hr;
                            datastruct{c}.data{2} = ptstruct(pt).data{i}.pulsep;
                            datastruct{c}.data{3} = ptstruct(pt).data{i}.lactate;
                            datastruct{c}.data{4} = ptstruct(pt).data{i}.uo;
                            datastruct{c}.data{5} = ptstruct(pt).data{i}.map;
                        end
                    end
                end
            end
        end
        
    otherwise
        datastruct = [];
        return;
end

nmin = [4 4 1 4 4];
tmin = [12*60 12*60 24*60 12*60 12*60];
ndata{5} = [];
for c = 1 : length(datastruct)
    for n = 1 : 5
        if ~isempty(datastruct{c}.data{n})
            ind = find(datastruct{c}.data{n}(:,1) < 24*60);
            if ~isempty(ind)
                if length(ind) >= nmin(n) && min(datastruct{c}.data{n}(ind,1)) <= tmin(n)
                    ndata{n} = [ndata{n} c];
                end
            end
        end
    end
end
 
% if dostats
%     switch whichgroup
%         case 'mort28d'
%             for i = 1 : 2
%                 los{i} = []; age{i} = [];
%                 ismale{i} = 0; isvaso{i} = 0;
%                 for pt = ids{i} % Survivors
%                     los{i} = [los{i} ptstruct(pt).los];
%                     age{i} = [age{i} ptstruct(pt).age];
%                     ismale{i} = ismale{i} + ptstruct(pt).ismale;
%                     isvaso{i} = isvaso{i} + ptstruct(pt).clinics.vp;
%                 end
%             end
%             fprintf('\nTotal number of patients = %d\n',N);
%             fprintf('Number of patients with 1 ICU adm = %d\n',N1);
%             fprintf('Number of patients with 1 ICU adm and data = %d\n',N2);
%             fprintf('\n Survivors (N = %d, %.1f%%):\n',length(ids{1}), ...
%                 length(ids{1})/(length(ids{1}) + length(ids{2}))*100);
%             fprintf('   LoS: %.1f (%.1f) [%.2f - %.2f]',mean(los{1}), ...
%                 std(los{1}),min(los{1}),max(los{1}));
%             fprintf('\n   Age: %.1f (%.1f) [%.2f - %.2f]',mean(age{1}), ...
%                 std(age{1}),min(age{1}),max(age{1}));
%             fprintf('\n   Gender (males): %d (%.1f%%)',ismale{1}, ...
%                 ismale{1}/length(ids{1})*100);
%             fprintf('\n   Vaso therapy: %d (%.1f%%)',isvaso{1}, ...
%                 isvaso{1}/length(ids{1})*100);
%             fprintf('\n');
%             fprintf('\n\n Non-Survivors (N = %d, %.1f%%):\n',length(ids{2}), ...
%                 length(ids{2})/(length(ids{1}) + length(ids{2}))*100);
%             fprintf('   LoS: %.1f (%.1f) [%.2f - %.2f]',mean(los{2}), ...
%                 std(los{2}),min(los{2}),max(los{2}));
%             fprintf('\n   Age: %.1f (%.1f) [%.2f - %.2f]',mean(age{2}), ...
%                 std(age{2}),min(age{2}),max(age{2}));
%             fprintf('\n   Gender (males): %d (%.1f%%)',ismale{2}, ...
%                 ismale{2}/length(ids{2})*100);
%             fprintf('\n   Vaso therapy: %d (%.1f%%)',isvaso{2}, ...
%                 isvaso{2}/length(ids{2})*100);
%             fprintf('\n\n');
%             
%         otherwise
%             ids = [];
%     end
% end

end